package xyz.worldzhile.choice.pre;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xyz.worldzhile.choice.entity.Subject;

import java.util.HashMap;
import java.util.Random;

/***
 * springboot bean 注册完毕  初始化题库加载
 */
@Component
@Order(2)
public class MyRuner implements ApplicationRunner {


    private String titleText="法国梵蒂冈梵都是发顺丰到付对妇女的洛克菲勒的房价大幅大锅饭大锅饭的" +
            "反对梵蒂冈反对大师傅离开了国际法的是";

    private Random random=new Random();

    private String[] rights={"A","B","C","D"};


    @Autowired
    private QuestionLibrary questionLibrary;


    @Override
    public void run(ApplicationArguments args) throws Exception {

        //springboot 去启动做的事情 读取配置 读取数据库连接
        // CommandLineRunner和ApplicationRunner。他们的执行时机为容器启动完成的时候

        System.out.println("------------初始化题库数据开始--------------");
        HashMap<Integer, Subject> initData = new HashMap<>();
        for (int i = 0; i <20 ; i++) {
            Subject subject = new Subject();
            subject.setNumber(i);
            subject.setTitle(getRandomTitle());
            subject.setSelectA("A:"+getRandomTitle());
            subject.setSelectB("A:"+getRandomTitle());
            subject.setSelectC("A:"+getRandomTitle());
            subject.setSelectD("A:"+getRandomTitle());
            subject.setSrighSelect(getRight());
            initData.put(subject.getNumber(),subject);
        }
        questionLibrary.setList(initData);
        System.out.println("------------初始化题库数据完成--------------");

    }




    public String getRandomTitle(){
        StringBuffer title = new StringBuffer();
        int charNum = getRandomNum();
        for (int i = 0; i <charNum ; i++) {
            int word = random.nextInt(titleText.length());
            title.append(titleText.charAt(word));
        }
        return title.toString();

    }

    public int getRandomNum(){
        int charNum = random.nextInt(20);
        while (charNum==0)
            charNum=getRandomNum();
        return charNum;
    }

    public String getRight(){
        int index = random.nextInt(rights.length);
        return rights[index];

    }
}
